package com.itheima.pinda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.pinda.DTO.transportline.TransportTripsDto;
import com.itheima.pinda.common.CustomIdGenerator;
import com.itheima.pinda.common.utils.Constant;
import com.itheima.pinda.entity.PdTransportTrips;
import com.itheima.pinda.mapper.PdTransportTripsMapper;
import com.itheima.pinda.service.PdTransportTripsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PdTransportTripsServiceImpl implements PdTransportTripsService {

    @Autowired
    private CustomIdGenerator idGenerator;
    @Autowired
    private PdTransportTripsMapper transportTripsMapper;

    @Override
    @Transactional
    public TransportTripsDto insert(TransportTripsDto transportTripsDto) {

        PdTransportTrips transportTrips = new PdTransportTrips();
        BeanUtils.copyProperties(transportTripsDto,transportTrips);
        if (StringUtils.isEmpty(transportTrips.getId())) {
            // 新增
            transportTrips.setId(idGenerator.nextId(transportTrips) + "");
            transportTripsMapper.insert(transportTrips);
            transportTripsDto.setId(transportTrips.getId());
        }else {
            // 修改
            transportTripsMapper.updateById(transportTrips);
        }
        return transportTripsDto;
    }

    @Override
    public TransportTripsDto selectById(String id) {
        PdTransportTrips transportTrips = transportTripsMapper.selectById(id);
        TransportTripsDto transportTripsDto = new TransportTripsDto();
        BeanUtils.copyProperties(transportTrips,transportTripsDto);
        return transportTripsDto;
    }

    @Override
    public List<TransportTripsDto> findAll(String transportLineId, List<String> ids) {
        List<PdTransportTrips> transportTrips =
                transportTripsMapper.selectList(new LambdaQueryWrapper<PdTransportTrips>()
                .eq(!StringUtils.isEmpty(transportLineId), PdTransportTrips::getTransportLineId, transportLineId)
                .in(!ids.isEmpty(), PdTransportTrips::getId, ids));

        List<TransportTripsDto> tripsDtoList = transportTrips.stream().map(pdTransportTrips -> {
            TransportTripsDto transportTripsDto = new TransportTripsDto();
            BeanUtils.copyProperties(pdTransportTrips, transportTripsDto);
            return transportTripsDto;
        }).collect(Collectors.toList());

        return tripsDtoList;
    }

    @Override
    public void delete(List<String> ids) {

        transportTripsMapper.update(null,
                new LambdaUpdateWrapper<PdTransportTrips>()
                        .set(PdTransportTrips::getStatus, Constant.DATA_DISABLE_STATUS)
                        .in(PdTransportTrips::getId,ids));
    }
}
